package com.maoslv.controller;

import com.maoslv.aop.LogAnnotation;
import com.maoslv.dto.Result;
import com.maoslv.pojo.Pagination;
import com.maoslv.pojo.Role;
import com.maoslv.pojo.User;
import com.maoslv.service.menu.MenuService;
import com.maoslv.service.role.RoleService;
import com.maoslv.service.user.UserService;
import com.maoslv.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Scope("request")
public class RoleController {
    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys:role:list')")
    @LogAnnotation(operator = "获取角色列表")
    public Result getRoleList(@RequestBody Pagination pagination) {
        Map<String, Object> map = new HashMap<>();
        map.put("name",pagination.getName());
        map.put("currentPage",(pagination.getCurrentPage() - 1) * 5);
        map.put("pageSize",pagination.getPageSize());
        Map<String,Object> result = roleService.getRoleList(map);
        if (result != null) {
            return Result.succ("获取角色列表成功", result);
        }
        return Result.fail("获取角色列表失败");
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('sys:role:update')")
    @LogAnnotation(operator = "更新角色")
    public Result updateRole(@RequestBody Role role) {
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("id",role.getId());
        roleMap.put("roleName",role.getRoleName());
        roleMap.put("code",role.getCode());
        roleMap.put("info",role.getInfo());
        if (roleService.updateRole(roleMap) > 0) {
            return Result.succ("编辑成功", null);
        }
        return Result.fail("编辑失败");
    }

    @PostMapping("/roleInfo")
    @PreAuthorize("hasAnyAuthority('sys:role:list')")
    @LogAnnotation(operator = "获取角色信息")
    public Result getRoleInfo(@RequestBody Integer id) {
        Role roleInfo = roleService.getRoleInfo(id);
        if (roleInfo != null) {
            return Result.succ("获取角色信息成功", roleInfo);
        }
        return Result.fail("获取失败");
    }

    @PostMapping("/delRole")
    @PreAuthorize("hasAuthority('sys:role:del')")
    @LogAnnotation(operator = "删除角色")
    public Result delRole(@RequestBody Integer id) {
        if (roleService.delRole(id) > 0) {
            return Result.succ("删除成功", null);
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/addRole")
    @PreAuthorize("hasAnyAuthority('sys:role:add')")
    @LogAnnotation(operator = "新增角色")
    public Result addRole(@RequestBody Role role) {
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("roleName",role.getRoleName());
        roleMap.put("code",role.getCode());
        roleMap.put("info",role.getInfo());
        if (roleService.addRole(roleMap) > 0) {
            return Result.succ("添加成功",null);
        }
        return Result.fail("添加失败");
    }

    @PostMapping("updatePerms/{id}")
    @PreAuthorize("hasRole('admin')")
    @LogAnnotation(operator = "更新用户权限")
    public Result updatePerms(@PathVariable Integer id, @RequestBody List<Integer> menuIds, Principal principal) {
        boolean flag = false;
        if (id == 1) {
            return Result.fail("管理员禁止修改");
        }
        if (roleService.updatePerms(id,menuIds) > 0) {
            User user = userService.getByUsername(principal.getName());
            List<Integer> roleId = userService.getRoleId(user.getId());
            for (Integer integer : roleId) {
                if (integer == id) {
                    flag = true;
                    break;
                }
            }
            redisUtil.del(id + "-jurisdiction");
            redisUtil.del(user.getUsername() + "-menuList");
            return Result.succ("分配权限成功",flag);
        }
        return Result.fail("分配权限失败");
    }
}
